class Solution:
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        l = len(prices)
        if l <= 1:
            return 0

        res = 0
        buy = prices[0]
        for (i, v) in enumerate(prices):
            if ((i + 1 > l - 1) or (prices[i + 1] <= v)) and v > buy:
                res += (v - buy)
                if i + 1 <= l - 1:
                    buy = prices[i + 1]
                    i += 1
                continue

            if v < buy:
                buy = v

        return res

s = Solution()
print('ss', s.maxProfit([2,1,4,5,2,9,7]))
print('ss', s.maxProfit([7,1,5,3,6,4]))
print('ss', s.maxProfit([1,2,3,4,5]))
print('ss', s.maxProfit([[7,6,4,3,1]]))

